package com.example.tys.baseapplication.util;

import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import com.example.tys.baseapplication.util.badge.Badger;
import com.example.tys.baseapplication.util.badge.BadgerType;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 角标
 *
 * @author tys
 * @date 2019/3/24
 */

public class BadgeUtils {

    private static final String TAG = "BadgeUtils";

    /**
     * 华为
     */
    private static final String HUA_WEI = "HUAWEI";

    /**
     * 小米
     */
    private static final String XIAO_MI = "XiaoMi";

    /**
     * 联想
     */
    private static final String ZUK = "ZUK";

    private static final String OPPO = "OPPO";
    private static final String VIVO = "VIVO";
    private static final String ZTE = "ZTE";
    private static final String MEZU = "Meizu";


    public static void setBadgeNum(Context context, int num) {
        Log.d(TAG, "制造商:" + android.os.Build.MANUFACTURER);
        String appLauncherClassName = getAppLauncherClassName(context);
        String phoneLauncherClassName = getPhoneLauncherClassName(context);
        Log.d(TAG, "app launch:" + appLauncherClassName + " phoneLauncherClassName:" + phoneLauncherClassName);

        Badger badger = getBadger(context);


        if (HUA_WEI.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
            setBadgeNumForHUAWEI(context, num);
        } else if (XIAO_MI.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
            setBadgeNumForXIAOMI(context, num);
        } else if (ZUK.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
            setBadgeNumForZUK(context, num);
        } else if (OPPO.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {

        } else if ("BBK".equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
            setBadgeNumForVIVO(context, num);
        } else if (ZTE.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
            setBadgeNumForZTE(context, num);
        } else {

        }
    }

    private static Badger getBadger(Context context) {
        String phoneLauncherClassName = getPhoneLauncherClassName(context);
        for (BadgerType badgerType : BadgerType.values()) {

        }
        return null;
    }

    private static void setBadgeNumForVIVO(Context context, int num) {
        Intent intent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
        intent.putExtra("packageName", context.getPackageName());
        Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        intent.putExtra("className", launchIntent.getComponent().getClassName());
        intent.putExtra("notificationNum", num > 99 ? 99 : num);
        context.sendBroadcast(intent);
    }

    private static void setBadgeNumForZTE(Context context, int num) {
        Bundle extra = new Bundle();
        extra.putInt("app_badge_count", num);
        Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        extra.putString("app_badge_component_name", launchIntent.getComponent().flattenToShortString());
        context.getContentResolver().call(Uri.parse("content://com.android.launcher3.cornermark.unreadbadge"),
                "setAppUnreadCount", null, extra);

    }

    /**
     * 联想 - 设置角标 <p>
     * <p>
     * 设置 > 通知和状态栏 > 应用角标管理 在应用列表里打开您的应用的角标使用权限
     * http://developer.zuk.com/detail/12
     * 声明权限：
     * <uses-permission android:name="android.permission.READ_APP_BADGE"/>
     *
     * @param context
     * @param num
     */
    private static boolean setBadgeNumForZUK(Context context, int num) {
        boolean isSupportedBade = true;
        try {
            Bundle extra = new Bundle();
            extra.putInt("app_badge_count", num);
            context.getContentResolver().call(Uri.parse("content://com.android.badge/badge"),
                    "setAppBadgeCount", null, extra);
        } catch (Exception e) {
            e.printStackTrace();
            isSupportedBade = false;
        }
        return isSupportedBade;
    }

    /**
     * 小米 - 设置角标 <p>
     * https://dev.mi.com/console/doc/detail?pId=939
     *
     * @param context
     * @param num
     * @return
     */
    private static boolean setBadgeNumForXIAOMI(Context context, int num) {
        boolean isSupportedBade = true;
        try {
            Notification notification = new Notification.Builder(context).build();
            Field field = notification.getClass().getDeclaredField("extraNotification");
            Object extraNotification = field.get(notification);
            Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);
            method.invoke(extraNotification, num);
        } catch (Exception e) {
            e.printStackTrace();
            isSupportedBade = false;
        }
        return isSupportedBade;
    }


    /**
     * 华为 - 设置角标 <p>
     * EMUI4.1 及以上 https://developer.huawei.com/consumer/cn/devservice/doc/30802 <p>
     * 声明权限：<p>
     * <uses-permission android:name="android.permission.INSTALL" />
     * <uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE" />
     *
     * @param context
     * @param num     为0时不显示角标 大于0时显示角标
     * @return false 不支持角标
     */
    private static boolean setBadgeNumForHUAWEI(Context context, int num) {
        boolean isSupportedBade = true;
        try {
            Bundle bundle = new Bundle();
            bundle.putString("package", context.getPackageName());
            String launcherClassName = getAppLauncherClassName(context);
            Log.d(TAG, "launcherClassName_:" + launcherClassName);
            bundle.putString("class", launcherClassName);
            bundle.putInt("badgenumber", num);
            context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"),
                    "change_badge", null, bundle);
        } catch (Exception e) {
            e.printStackTrace();
            isSupportedBade = false;
        }
        return isSupportedBade;
    }

    /**
     * 获取应用的launcher name
     *
     * @param context
     * @return
     */
    private static String getAppLauncherClassName(Context context) {
        PackageManager packageManager = context.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setPackage(context.getPackageName());
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
        if (info == null) {
            info = packageManager.resolveActivity(intent, 0);
        }
        return info.activityInfo.name;
    }

    /**
     * 获取手机的launcher name
     *
     * @param context
     * @return
     */
    private static String getPhoneLauncherClassName(Context context) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
        if (resolveInfo != null) {
            return resolveInfo.activityInfo.packageName;
        } else {
            return "";
        }
    }

}
